<?php
/**
 * Created by PhpStorm.
 * User: Duck
 * Create time: 2016/12/21 9:56
 */

namespace D\Controller;
use Common\Controller\BaseController;

class StudentController extends BaseController{
    public function show(){
        $pages = ceil(M('students')->count()/4);
        $a=M('students')-> page(I('get.p'),4)->select();
        $c=M('students')->select();
        $this->assign('navs',$a);
        $this->assign('addnavs',$c);
        $this->assign('pages',$pages);
        $this->display();
    }
    public function getShow(){
        $pages = ceil(M('students')->count()/4);
        $res=M('students')
            ->page(I('get.p'),4)
            ->select();
        $this->ajaxReturn($res);
    }
    public function doDelStudent(){
        M('dict')->delete(I('post.ids'));
        $res = [
            'msg'=>'删除成功',
            'error'=>0
        ];
        $this->ajaxReturn($res);
    }
    public function photoUpload(){
        $this->assign('id',I('get.id'));
        $this->display('Student/portiait');
    }
    public function editStudent(){
        $where=[
            'student_id'=>I('get.id'),
        ];
        $res=M('students')->where($where)->find();
        $arr=explode(',',$res['learn_way']);
        $this->assign('res',$res);
        $this->assign('arrs',$arr);
        $this->display();
    }
    public function doEditStudent(){
        $_POST['learn_way']= implode(',',$_POST['learn_way']);
        if(D('students')->create()){
            D('students') ->save();
            $url=U('Index/success?url=Student/show');
            header('Location:'.$url);
        }else{
            $url=U('Index/defeat?url=Student/show');
            header('Location:'.$url);
        }
    }
    public function doAddSave(){
        $_POST['learn_way']= implode(',',$_POST['learn_way']);
        if(D('students')->create()){
            $id=D('students') ->add();
            echo $id;
            $this->assign('id',$id);
            $this->display('Student/portiait');
        }else{
            $url=U('Index/defeat?url=Student/addStudent');
            header('Location:'.$url);
        }
    }
    public function addStudent(){
        $this->display();
    }
    public function a(){
        $res=M('students')->find();
        $ab=$this->saveBase64Image($res['big_photo']);
        dump($ab);
    }
}
